/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

<template>
  <div class='container'>
    <div class='section'>
      <div class='content'>
        <div>加载本地H5页面，点击下方按钮调用Nodejs服务器API，服务器返回信息显示在日志区。</div>
        <div>页面路径：</div>
        <div>/data/storage/el1/bundle/entry/resource/resfile/LocalResource/dist/index.html</div>
        <div>接口路径：</div>
        <div>http://www.a.harmonyos:8080/api/echo</div>
      </div>
      <div class='logArea'>
        <div class='logArea_name'>日志区</div>
        <div v-if="result" :class="['result', success ? 'success' : 'error']">
          {{ result }}
        </div>
      </div>
    </div>
    <div class='footer'>
      <div class='btn' @click="fetchData">点击请求</div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      result: null,
      success: false
    };
  },
  methods: {
    async fetchData() {
      try {
        const response = await axios.get('http://www.a.harmonyos:8080/api/echo', {
          params: {
            content: '来自本地Vue的消息'
          }
        });
        const resStr = JSON.stringify(response.data, null, 2);
        if (resStr.includes('<title>Error</title>')) {
          this.success = false;
        } else {
          this.success = true;
        }
        this.result = response.data;
      } catch (error) {
        this.result = `请求失败: ${error.message}`;
        this.success = false;
      }
    }
  }
};
</script>

<style scoped>
  .container {
    padding: 16px 16px 32px;
    max-width: 750px;
    height: calc(100vh - 56px);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    overflow-y: auto;
  }
  .section .content {
    font-size: 16px;
    line-height: 1.6;
    word-wrap: break-word;
  }
  .logArea {
    margin: 16px 0px 32px;
    box-sizing: border-box;
    padding: 16px 16px;
    width: 100%;
    min-height: 200px;
    max-height: 350px;
    overflow-y: auto;
    border-radius: 20px;
    background-color: #ffffff;
    box-shadow: 0 0 4px rgba(0,0,0,0.1);
  }
  .result {
    margin-top: 15px;
    padding: 15px;
    border-radius: 4px;
    background-color: #faf9fa;
    white-space: pre-wrap;
    font-family: monospace;
    overflow-x: auto;
  }
  .success {
    border: 1px solid #42b983;
  }
  .error {
    border: 1px solid #ff4444;
    color: #ff4444;
  }
  .btn {
    width: 100%;
    height: 40px;
    border-radius: 20px;
    background-color: #0a59f7;
    display: inline-block;
    color: #f1f3f5;
    text-align: center;
    line-height: 40px;
  }
</style>